package pack.soldierboom;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
 

public class GameView extends SurfaceView
{
	
	float cx;
	float cy;
	
	private Bitmap bmp;
    private SurfaceHolder holder;
    private GameLoopThread gameLoopThread;
    private List<Sprite> sprites = new ArrayList<Sprite>();
    private Bomba bomba;
    private long ultimoClick;
    //private Bomba2 bomba;
      
    public GameView(Context context) 
    {
    	super(context);
        gameLoopThread = new GameLoopThread(this);
        holder = getHolder();
        holder.addCallback(new SurfaceHolder.Callback()
        {
        	@Override
            public void surfaceDestroyed(SurfaceHolder holder)
            {
        		boolean retry = true;
                gameLoopThread.setRunning(false);
                while (retry)
                {
                	try
                	{
                		gameLoopThread.join();
                        retry = false;
                    }
                	catch (InterruptedException e)
                	{
                		
                	}
                }
               }
        	@Override
        	public void surfaceCreated(SurfaceHolder holder)
        	{
        		createSprites();
        		gameLoopThread.setRunning(true);
                gameLoopThread.start();
            }
 
            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,int width, int height)
            {
            	
            }
           });
        bomba = new Bomba(context,this);
                    
       }
    


	private void createSprites()
    {
		for(int i =0; i<10;i++)
		{
    	sprites.add(createSprite(R.drawable.soldier));
    	}    
    	sprites.add(createSprite(R.drawable.soldier2));
    }
    


	private Sprite createSprite(int resouce)
    {
		Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
    	return new Sprite(this,bmp);
    	
    }
 


    
	@SuppressLint("WrongCall")
	@Override
	public void draw(Canvas canvas) 
       {
             canvas.drawColor(Color.BLUE);
             for(Sprite sprite : sprites)
             {
            	 sprite.onDraw(canvas); 
             }             
             canvas.drawCircle(cx, cy, bomba.radius, bomba.paint);
       }
	
 /*
	public boolean onTouchEvent(MotionEvent event)
    {
        if (System.currentTimeMillis() - ultimoClick > 500)
        {
               ultimoClick = System.currentTimeMillis();
               synchronized (getHolder())
               {
                   for (int i = sprites.size() - 1; i >= 0; i--)
                   {
                       Sprite sprite = sprites.get(i);
                       if (sprite.isCollition(event.getX(), event.getY()))
                       {
                    	   
                             sprites.remove(sprite);
                             break;
                       }
                   }
               }
        }
        return true;
  }*/
	public boolean onTouchEvent(MotionEvent event)
	{
		
		if (event.getAction() == MotionEvent.ACTION_DOWN) 
		{
			Log.i(Main.TAG, "down baby down !! ");
			cx = event.getX();
			cy = event.getY();
			
			  if (System.currentTimeMillis() - ultimoClick > 500)
		        {
		               ultimoClick = System.currentTimeMillis();
		               synchronized (getHolder())
		               {
		                   for (int i = sprites.size() - 1; i >= 0; i--)
		                   {
		                       Sprite sprite = sprites.get(i);
		                       if (sprite.isCollition(event.getX(), event.getY()))
		                       {
		                    	   
		                             sprites.remove(sprite);
		                             break;
		                       }
		                   }
		               }
		        }
		}

		if (event.getAction() == MotionEvent.ACTION_MOVE)
		{
			Log.i(Main.TAG, "SHAKE !!!");
			cx = event.getX();
			cy = event.getY();
		}

		if (event.getAction() == MotionEvent.ACTION_UP)
		{
			Log.i(Main.TAG, "nao encoste !!");
		}

		return super.onTouchEvent(event);
	}
	
	
}